package com.woniu.woniuk16.securityUtil;

import com.woniu.woniuk16.dto.WoniuUserDetails;
import com.woniu.woniuk16.entity.WoniuAccount;
import com.woniu.woniuk16.exception.stu.PowerException;
import com.woniu.woniuk16.mapper.WoniuResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class SafeFilter extends OncePerRequestFilter {

    private AntPathMatcher apm;
    private LoginErrorHandler leh;
    private RedisTemplate<String,Object> rtl;
    private WoniuResourceMapper woniuResourceMapper;
    public SafeFilter(){

    }
    public SafeFilter(LoginErrorHandler leh,RedisTemplate<String,Object> rtl,WoniuResourceMapper woniuResourceMapper){
        this.apm = new AntPathMatcher();
        this.leh = leh;
        this.rtl = rtl;
        this.woniuResourceMapper = woniuResourceMapper;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        WoniuUserDetails woniuUserDetails =null;
        try {
            woniuUserDetails = (WoniuUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        }catch (Exception e){
            filterChain.doFilter(httpServletRequest,httpServletResponse);
            return;
        }


        if (woniuUserDetails==null){
            filterChain.doFilter(httpServletRequest,httpServletResponse);
            return;
        }

        Integer accountId = woniuUserDetails.getAccountId();



        String requestURI = httpServletRequest.getRequestURI();

        Object obj = rtl.opsForValue().get("woniu:resource:"+accountId);


        if (obj==null){
            List<String> url = woniuResourceMapper.getUrl(accountId);
            rtl.opsForValue().set("woniu:resource:"+accountId,url);
            rtl.expire("woniu:resource:"+accountId,30, TimeUnit.MINUTES);
            obj = url;
        }

        List<String> resource = (List<String>) obj;
        System.out.println(resource);
        boolean flag = false;

        for (String s : resource) {
            if (apm.match(s, requestURI)) {
                flag = true;
                break;
            }
        }
        if (flag){
            filterChain.doFilter(httpServletRequest,httpServletResponse);
        }else{
            leh.onAuthenticationFailure(httpServletRequest,httpServletResponse,new PowerException("没有权限"));
        }
//            filterChain.doFilter(httpServletRequest,httpServletResponse);

    }



}
